#include "stdafx.h"
#include "CAnimal.h"
#include "CWolf.h"
#include <iostream>
#include <fstream>
using namespace std;

CAnimal::CAnimal()
{
}


CAnimal::~CAnimal()
{
}

CAnimal::CAnimal(int pg, int pa, int pb)
{
	m_gender = pg;
	m_age = pa;
	m_blood = pb;
}

void CAnimal::SetAnimal(int pg, int pa, int pb,int x,int y)
{
	m_gender = pg;
	m_age = pa;
	m_blood = pb;
	m_pos.x = x;
	m_pos.y = y;
}

void CAnimal::LoadImag()
{
	m_animalimg = cvLoadImage("./picture/wolf0.jpg");
}

void CAnimal::ShowAnimal()
{
	cout << "This is an animal" << endl;
	if (m_gender == 0)
		cout << "The animal is male. It is " << m_age << " years old."
		<< "Its blood level is " << m_blood << endl;
	else
		cout << "The animal is female. It is " << m_age << " years old."
		<< "Its blood level is " << m_blood << endl;
}

int CAnimal::BloodReduce()
{
	return 1;
}

/*int CAnimal::Scope()
{
	return m_distance;
}*/

void CAnimal::Eat(CAnimal *anim)
{
	if (typeid(*anim) == typeid(CWolf))
		CWolf *wolf = dynamic_cast<CWolf *>(anim);
	else
		CSheep *sheep = dynamic_cast<CSheep *>(anim);
}

void CAnimal::Print(int flag, int i)
{
	fstream file("./results.txt", ios::app);
	if (file.is_open())
	{
		if (flag == 0)
			file << "Animal " << (i + 1) << " was dead!" << endl;
		else
			file << "Animal " << (i + 1) << " is alive!" << endl;
	}
	file.close();
}

void CAnimal::DrawtoBk(IplImage * pbk)
{
	if (pbk != NULL && m_animalimg != NULL)
	{
		for (int i = 0; i < m_animalimg->height; i++)
		{
			for (int j = 0; j < m_animalimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 2);
				if (!(bch > 245 && gch > 245 && rch > 245))
				{
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 0) = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 0);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 1) = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 1);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 2) = CV_IMAGE_ELEM(m_animalimg, uchar, i, j * 3 + 2);
				}
			}
		}
	}
	else
		cout << "pbk is null";
}

void CAnimal::ReleaseImg()
{
	cvReleaseImage(&m_animalimg);
}
